package com.zyf.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zyf.pojo.SmbmsUser;
import com.zyf.service.SmbmsUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张忆菲
 * @since 2020-09-09
 */
@Controller
public class SmbmsUserController {

    @Autowired
    SmbmsUserService service;

    @GetMapping({"/","index","/toLogin"})
    public String index(){
        return "login";
    }



    @PostMapping("/login")
    public String login(String username,String password,Model model){
        //获取当前的用户
        Subject subject = SecurityUtils.getSubject();
        //封装用户的登录数据
        UsernamePasswordToken token = new UsernamePasswordToken(username,password);
        try {
            subject.login(token); //执行登录方法
            return "main";
        }catch (UnknownAccountException e){//判断用户名
            model.addAttribute("message","用户名错误");
            return "login";
        }catch (IncorrectCredentialsException e){ //判断密码
            model.addAttribute("message","密码错误");
            return "login";
        }

    }
    @GetMapping("/logout")
    public String logout(){
        //获取当前的用户
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "login";
    }

    @GetMapping("/user/findAll")
    public String findAll(Model model){
        List<SmbmsUser> list = service.list(null);
        model.addAttribute("users",list);
        return "user/userList";
    }
    @PostMapping("/user/findLike")
    public String findLike(SmbmsUser user, Model model){
        QueryWrapper<SmbmsUser> wrapper = new QueryWrapper<>();
        wrapper.like("name",user.getName());
        List<SmbmsUser> list = service.list(wrapper);
        model.addAttribute("users",list);
        return "user/userList";
    }

    @GetMapping("/user/toAdd")
    public String toAdd(){
        return "user/userAdd";
    }

    @PostMapping("/user/add")
    public String add(SmbmsUser user,Model model){
        boolean b = service.save(user);
        if(b==false){
            model.addAttribute("message","添加失败");
            return "user/userAdd";
        }
        return "redirect:/user/findAll";
    }

    @GetMapping("/user/toUpdate/{userId}")
    public String toUpdate(@PathVariable("userId") Integer userId,Model model){
        SmbmsUser byId = service.getById(userId);
        model.addAttribute("user",byId);
        return "user/userUpdate";
    }

    @PostMapping("/user/update")
    public String update(SmbmsUser user,Model model){
        boolean b = service.updateById(user);
        if(b==false){
            model.addAttribute("message","修改失败");
            return "user/userUpdate";
        }
        SmbmsUser serviceById = service.getById(user.getUserId());
        model.addAttribute("user",serviceById);
        return "user/userView";
    }

    @PostMapping("/user/delete")
    public String delete(SmbmsUser user ,Model model){
        boolean b = service.removeById(user.getUserId());
        if(b==false){
            model.addAttribute("message","删除失败");
            return "user/userView";
        }
        return "redirect:/user/findAll";
    }

    @GetMapping("/user/toUpdatePwd")
    public String toUpdatePwd(){
        return "user/updatePwd";
    }

    @PostMapping("/user/updatePwd")
    public String updatePwd(String oldPassword,String newPassword,SmbmsUser user,Model model){
        QueryWrapper<SmbmsUser> wrapper = new QueryWrapper<>();
        wrapper.eq("loginpwd",oldPassword);
        SmbmsUser one = service.getOne(wrapper);
        if (one==null){
            model.addAttribute("message","旧密码不存在");
            return "user/updatePwd";
        }
        if (oldPassword.equals(newPassword)){
            model.addAttribute("message","旧密码和新密码一致");
            return "user/updatePwd";
        }
        QueryWrapper<SmbmsUser> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("user_id",user.getUserId());
        user.setLoginpwd(newPassword);
        boolean b = service.update(user, wrapper2);
        if(b==false){
            model.addAttribute("message","修改失败");
            return "user/updatePwd";
        }
        return "login";
    }
}

